
<div class="layui-card layadmin-header">
  <div class="layui-breadcrumb" lay-filter="breadcrumb">
    <a lay-href="">主页</a>
    <a><cite>支付订单</cite></a>
  </div>
</div>

<div class="layui-fluid">
  <div class="layui-card">
    <div class="layui-tab layui-tab-brief">
      <ul class="layui-tab-title">
        <li class="layui-this">支付订单</li>
      </ul>
      <div class="layui-tab-content">
        <div class="layui-tab-item layui-show">
          <div class="layui-row">
            <div class="layui-form" style="float:right; margin-bottom: 10px;">
              <div class="layui-form-item" style="margin:0;">
                <div class="layui-input-inline">
                  <input type="text" name="createTimeStart" id="createTimeStart" autocomplete="off" placeholder="开始时间" class="layui-input">
                </div>
                <div class="layui-input-inline">
                  <input type="text" name="createTimeEnd" id="createTimeEnd" autocomplete="off" placeholder="结束时间" class="layui-input">
                </div>
                <div class="layui-input-inline">
                  <input type="text" name="mchId" id="mchId" placeholder="商户ID" autocomplete="off" class="layui-input">
                </div>
                <div class="layui-input-inline">
                  <input type="text" name="payOrderId" id="payOrderId" placeholder="支付订单号" autocomplete="off" class="layui-input">
                </div>
                <div class="layui-input-inline">
                  <input type="text" name="mchOrderNo" id="mchOrderNo" placeholder="商户订单号" autocomplete="off" class="layui-input">
                </div>
                <div class="layui-input-inline">
                  <select name="productId" id="productId" lay-search="">
                    <option value="-99">支付产品</option>
                  </select>
                </div>
<!--                <div class="layui-input-inline">-->
<!--                  <select name="type" id="productType" placeholder="产品类型" lay-search="">-->
<!--                    <option value="-99">产品类型</option>-->
<!--                    <option value="1">收款</option>-->
<!--                    <option value="2">充值</option>-->
<!--                  </select>-->
<!--                </div>-->
                <div class="layui-input-inline">
                  <select name="status" id="status" lay-search="">
                    <option value="-99">订单状态</option>
                    <option value="0">订单生成</option>
                    <option value="1">支付中</option>
                    <option value="2">支付成功</option>
                    <option value="-1">支付失败</option>
                    <option value="3">处理完成</option>
                    <option value="4">已退款</option>
                  </select>
                </div>
                <div class="layui-input-inline">
                  <button id="search" class="layui-btn" data-type="reload">搜索</button>
                </div>
                <div class="layui-input-inline">
                  <input type="checkbox" name="close" lay-filter="refreshSwitch" layui-tip="" lay-skin="switch" lay-text="自动刷新开|自动刷新关">
                </div>
              </div>
            </div>
            <div class="layui-form-item">
              <blockquote class="layui-elem-quote" id="amountTip">
                提交订单数:<sapn id="allTotalCount" style="color: dodgerblue; margin-right: 10px;"></sapn>
                订单总金额:<span id="allTotalAmount" style="color: blueviolet; margin-right: 10px;"></span>
                已付订单数:<span id="successTotalCount" style="color: blue; margin-right: 10px;"></span>
                已付总金额:<span id="successTotalAmount" style="color: blue; margin-right: 10px;"></span>
<!--                商户总收入:<span id="successTotalMchIncome" style="color: green; margin-right: 10px;"></span>-->
<!--                代理商收入:<span id="successTotalAgentProfit" style="color: forestgreen; margin-right: 10px;"></span>-->
<!--                平台收入:<span id="successTotalPlatProfit" style="color: darkgreen; margin-right: 10px;"></span>-->
                未付订单数:<span id="failTotalCount" style="color: red; margin-right: 10px;"></span>
                未付总金额:<span id="failTotalAmount" style="color: orangered; margin-right: 10px;"></span>
                成功率:<span id="sucRate" style="color: green; margin-right: 10px;"></span>
              </blockquote>
            </div>
          </div>
          <table id="XxPay_Mgr_PayOrder_dataAll" lay-filter="XxPay_Mgr_PayOrder_dataAll"></table>
        </div>

      </div>
    </div>
  </div>
</div>

<script type="text/html" id="xxpayBar">

  {{#  if(d.status == 1){ }}
  <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="reissue">补单</a>
  {{#  } else { }}
  <a class="layui-btn layui-btn-primary layui-btn-xs layui-btn-disabled" lay-event="reissue">补单</a>
  {{#  } }}
</script>

<script>
layui.use(['admin', 'table', 'util', 'laydate'], function(){
  var $ = layui.$
  ,admin = layui.admin
  ,table = layui.table
  ,element = layui.element
  ,laydate = layui.laydate
  ,form = layui.form;
  
  element.render('breadcrumb', 'breadcrumb');

  $.setCountResult = function(createTimeStart, createTimeEnd, mchId, productId, payOrderId, mchOrderNo, productType, status) {
    admin.req({
      type: 'get',
      url: layui.setter.baseUrl + '/pay_order/count',
      data: {
        mchId : mchId,
        productId : productId,
        payOrderId : payOrderId,
        mchOrderNo : mchOrderNo,
        productType : productType,
        status : status,
        createTimeStart : createTimeStart,
        createTimeEnd : createTimeEnd
      },
      error: function(err){
        layer.alert(JSON.stringify(err.field), {
          title: '错误提示'
        })
      },
      success: function(res){
        if(res.code == 0){
          $('#allTotalCount').html(res.data.allTotalCount);
          $('#allTotalAmount').html("￥" + res.data.allTotalAmount/100.00);
          $('#successTotalCount').html(res.data.successTotalCount);
          $('#successTotalAmount').html("￥" + res.data.successTotalAmount/100.00);
          // $('#successTotalMchIncome').html("￥" + res.data.successTotalMchIncome/100.00);
          // $('#successTotalAgentProfit').html("￥" + res.data.successTotalAgentProfit/100.00);
          // $('#successTotalPlatProfit').html("￥" + res.data.successTotalPlatProfit/100.00);
          $('#failTotalCount').html(res.data.failTotalCount);
          $('#failTotalAmount').html("￥" + res.data.failTotalAmount/100.00);
          $('#sucRate').html((res.data.successTotalCount/res.data.allTotalCount * 100).toFixed(2) + "%");
        }
      }
    });
  };

  // 加载支付产品
  admin.req({
    type: 'post',
    url: layui.setter.baseUrl + '/config/common/pay_product_all',
    error: function(err){
      layer.alert(err);
    },
    success: function(res){
      if(res.code == 0){
        var payProductList = res.data;
        //遍历赋值
        for(var i in payProductList){
          $("#productId").append('<option value= '+payProductList[i].id+'>'+payProductList[i].productName+'</option>');
        }
        form.render('select');
      }
    }
  });

  var tplStatus = function (d) {
    if(d.status == 0) {
      return "<span style='color: yellowgreen'>订单生成</span>";
    }else if(d.status == 1) {
      return "<span style='color: orangered'>支付中</span>";
    }else if(d.status == 2) {
      return "<span style='color: blueviolet'>支付成功</span>";
    }else if(d.status == 3) {
      return "<span style='color: blue'>处理完成</span>";
    }else if(d.status == -1) {
      return "<span style='color: darkred'>支付失败</span>";
    }else if(d.status == -2) {
      return "<span style='color: red'>订单过期</span>";
    }
  };

  var tplPaySuccTime = function (d) {
    layer.alert(layui.util.toDateString(d.createTime, "HH:mm:ss"));
    if(d.paySuccTime){
      return layui.util.toDateString(d.paySuccTime, "HH:mm:ss")
    }else{
      return "";
    }
  }

  var dateTime = new Date();
  dateTime.setDate(dateTime.getDate()-1);
  dateTime = new Date(dateTime);
  var startTimeDefault = layui.util.toDateString(dateTime, "yyyy-MM-dd 00:00:00")
  // 初始化统计结果
  $.setCountResult(startTimeDefault);

  //用户列表
  table.render({
    elem: '#XxPay_Mgr_PayOrder_dataAll'
    ,url: layui.setter.baseUrl + '/pay_order/list' //用户列表接口
    ,where: {
        access_token: layui.data(layui.setter.tableName).access_token,
        createTimeStart: startTimeDefault
    }
    ,id: 'tableReload'
    ,page: true
    ,limit: 30
    ,limits: [30, 50, 80, 100]
    ,cols: [[
      //{type: 'checkbox', fixed: 'left'}
     {field: 'mchId', title: '商户ID', width: 160}
      ,{field: 'mchName', title: '商户名称', width: 160}
      // ,{field: 'passageId', title: '通道ID'}
      ,{field: 'mchOrderNo', title: '商户单号', width: 220}
      ,{field: 'payOrderId', title: '支付单号', width: 220}
      , {field: 'productName', title: '产品名称', width: 160}
      ,{field: 'amount', title: '支付金额', width: 160, templet: '<div>{{ d.amount/100 }}</div>'}
      // ,{field: 'productType', title: '产品类型', templet: '<div>{{ d.productType == 1 ? "收款" : d.productType == 2 ? "充值" : ""}}</div>'}
      ,{field: 'status', title: '状态', width: 160, templet: tplStatus}
      ,{field: 'createTime', title: '创建时间', width: 160, templet: '<div>{{ d.createTime }}</div>'}
      ,{field: 'paySuccTime', title: '成功时间', width: 150, templet: tplPaySuccTime}
      ,{field: 'edit', title: '操作', width: 120,toolbar: '#xxpayBar' }
    ]]
    ,skin: 'line'
  });

  //监听工具条
  table.on('tool(XxPay_Mgr_PayOrder_dataAll)', function(obj){
    var data = obj.data;
    if(obj.event === 'detail'){
      location.href = layui.setter.baseLocal + "order/pay/view.html?" + data.payOrderId;
    }else if(obj.event === 'reissue') {
      var payOrderId = data.payOrderId;
      layer.prompt({
        formType: 1,
        title: '确认补单，请输入超级密码：'
      }, function(value, index, elem){
        layer.close(index);
        var load = layer.msg('重新发送中...', {
          icon: 16
          ,shade: 0.01
          ,time: 9999999999
        });
        admin.req({
          type: 'post',
          url: layui.setter.baseUrl + '/pay_order/reissue',
          timeout: 1000 * 60,
          data:{
            payOrderId: payOrderId,
            password: value
          },
          error: function(err){
            layer.close(load);
            layer.alert(err);
          },
          success: function(res){
            layer.close(load);
            if(res.code == 0){
              layer.alert("补单完成，请注意查看状态！");
              table.reload("tableReload");
            }
          }
        });
      });
    }
  });

  //监听指定开关
  form.on('switch(refreshSwitch)', function (data) {
    if (this.checked) {
      autoRefresh();
      layer.msg('自动刷新已开启,每间隔30秒刷新一次列表数据');
    } else {
      clearInterval(localStorage.refreshInterval);
      layer.msg('自动刷新已停止');
    }
  });

  clearInterval(localStorage.refreshInterval);
  localStorage.refreshInterval = null;
  // 自动刷新逻辑
  var autoRefresh = function () {
    var refreshTime = 30;
    localStorage.refreshInterval = setInterval(function () {
      if(layui.router().href == "/order/pay/"){
        $("#search").click();
      }
    }, refreshTime * 1000);
  };

  // 搜索
  var $ = layui.$, active = {
    reload: function(){
      var mchId = $('#mchId').val();
      var productId = $('#productId').val();
      var payOrderId = $('#payOrderId').val();
      var mchOrderNo = $("#mchOrderNo").val();
      var productType = $("#productType").val();
      var createTimeStart = $('#createTimeStart').val();
      var createTimeEnd = $('#createTimeEnd').val();
      var status = $("#status").val();

      $.setCountResult(createTimeStart, createTimeEnd, mchId, productId, payOrderId, mchOrderNo, productType, status);
      //执行重载
      table.reload('tableReload', {
        page: {
          curr: 1 //重新从第 1 页开始
        }
        ,where: {
          mchId: mchId,
          productId: productId,
          payOrderId: payOrderId,
          mchOrderNo: mchOrderNo,
          productType: productType,
          status : status,
          createTimeStart : createTimeStart,
          createTimeEnd : createTimeEnd
        }
      });
    }
  };
  $('#search').on('click', function() {
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';
  });

  laydate.render({
    elem: '#createTimeStart'
    ,type: 'datetime'
    ,value: startTimeDefault
    ,format: 'yyyy-MM-dd HH:mm:ss'
  });

  laydate.render({
    elem: '#createTimeEnd'
    ,type: 'datetime'
    ,format: 'yyyy-MM-dd HH:mm:ss'
  });
  // 渲染表单
  form.render();
});
</script>